package com.spring.demo.rabbit.config;

import com.spring.demo.rabbit.ExchangeConst;
import com.spring.demo.rabbit.QueueConst;
import com.spring.demo.rabbit.RoutingKeyConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/***
 * @title FanoutExchangeConfig
 * @description : 主题模式： 通过Routing Key 和 交换器内binding队列的Routing key 进行匹配， “#”匹配0个或多个， “*”只匹配一个
 * @author baiji
 * @version 1.0.0
 * @create 2023/9/4 11:13
 **/
@Configuration
public class TopicExchangeConfig {

    @Resource
    private AmqpAdmin amqpAdmin;

    /**
     * topic 交换器
     *
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        TopicExchange exchange = ExchangeBuilder.topicExchange(ExchangeConst.TOPIC_EXCHANGE).build();
        amqpAdmin.declareExchange(exchange);
        Queue queue_01 = QueueBuilder.durable(QueueConst.TOPIC_QUEUE_01).build();
        amqpAdmin.declareQueue(queue_01);
        Queue queue_02 = QueueBuilder.durable(QueueConst.TOPIC_QUEUE_02).build();
        amqpAdmin.declareQueue(queue_02);
        Queue queue_03 = QueueBuilder.durable(QueueConst.TOPIC_QUEUE_03).build();
        amqpAdmin.declareQueue(queue_03);
        BindingBuilder.bind(queue_01).to(exchange).with(RoutingKeyConst.TOPIC_ROUTING_KEY_01);
        BindingBuilder.bind(queue_02).to(exchange).with(RoutingKeyConst.TOPIC_ROUTING_KEY_02);
        BindingBuilder.bind(queue_03).to(exchange).with(RoutingKeyConst.TOPIC_ROUTING_KEY_03);
        return exchange;
    }
}
